﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeaven.Web.Common;
using FortuneHeavenBAL;
using FortuneHeavenCommon.DataMap;

namespace FortuneHeaven.Web.Controls
{
    public partial class LoginPanel : System.Web.UI.UserControl
    {
        public delegate void LoginSuccessHandler();
        public event LoginSuccessHandler LoginSuccess;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (AuthorityCenter.CurrentUserID >= 0)
                {
                    ShowLoginUser();
                  
                }

                else
                {

                    ShowUnLogin();
                }
            }
        }

        public void RaiseSucces()
        {
            if (LoginSuccess != null)
            {
                LoginSuccess();
            }
        }

        public void LoginUser(object sender, EventArgs arg)
        {
            string error = "";
            if (!AuthorityCenter.ValidateUser(tbAccount.Text, tbPassword.Text, tbValidateCode.Text, ref error))
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "error", string.Format("alert('{0}');", error), true);
            }
            else
            {
                ClearLoginInput();
                ShowLoginUser();
                RaiseSucces();
            }
        }
        public void ClearLoginInput()
        {
            this.tbAccount.Text = "";
            this.tbValidateCode.Text = "";
        }

        public void ShowLoginUser()
        {
            this.divLogin.Visible = false;
            divAfterLogin.Visible = true;
            this.lbID.Text = AuthorityCenter.CurrentGameID.ToString();
            this.lbName.Text = AuthorityCenter.CurrentUserName.ToString();

           
            UserInforBO bo = new UserInforBO();
            UserInfor infor = bo.GetUserInfor(AuthorityCenter.CurrentUserID);
            AccountsInfoBO acbo = new AccountsInfoBO();
            GameScoreInfoBO gamescorebo = new GameScoreInfoBO();
            GameScoreInfo score = gamescorebo.GetGameScore(AuthorityCenter.CurrentUserID);
            AccountsInfo acount = acbo.GetLightAcountInfor(AuthorityCenter.CurrentUserName);
            int faceid = 0;
            if (infor != null)
            {
                faceid = infor.FaceID;
                this.lbTreasure.Text = infor.FortuneValue.ToString();
                this.lbSpread.Text = infor.SpreadValue.ToString();
            }
            else
            {
                this.lbTreasure.Text = "0";
                this.lbSpread.Text = "0";
            }

            if (score != null)
            {
                this.lbGameScore.Text = score.Score.ToString();
            }
            else
            {
                this.lbGameScore.Text = "0";
            }


            imgHead.ImageUrl = Page.ResolveUrl(string.Format("~/Pages/ImageHandler.ashx?pureheadid={0}&gender={1}", faceid, acount != null ? acount.Gender : 0));
        }

        public void ShowUnLogin()
        {
            this.divLogin.Visible = true;
            divAfterLogin.Visible = false;
        }

        public void UnLogin(object sender, EventArgs arg)
        {
            AuthorityCenter.Unlogin();
        
            Response.Redirect(Request.Url.OriginalString);
        }
    }
}